package com.app.xinfadi.ui.purchase_plan.center_purchase_plan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseActivity;
import com.app.xinfadi.dao.HomeDao;
import com.app.xinfadi.dao_bean.CenterPurchasePlanListBean;
import com.app.xinfadi.dao_bean.bean.CenterPurchasePlanBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.center_role.CenterRoleExamineAndApproveListRefreshBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.purchase_man.PurchaseChangeAddRefreshBean;
import com.app.xinfadi.ui.MainActivity;
import com.app.xinfadi.ui.home.view.GridViewImageNomalAdapter;
import com.app.xinfadi.ui.purchase_price.PurchasePriceDetailsActivity;
import com.app.xinfadi.util.Toast;
import com.app.xinfadi.widget.MyGridView;
import com.app.xinfadi.widget.gson.Success;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.MaterialDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * function: 中心采购报价详情
 * Created by Wutengfei on 2016/1/25.
 */
public class CenterPurchasePlanDetailActrvity extends BaseActivity {
    private Button newBillOfSalesBtn;
    //查看原始报价单
    private Button OriginPlansBtn;
    private int type;
    private String status = "-1";
    private Button recallBtn;
    private CenterPurchasePlanBean centerPurchasePlanBean;
    //市场
    private TextView producingAreaRbtn;
    /*基地*/
    private TextView homeBaseTv;
    /*产品名称*/
    private TextView productNameTv;
    /*可采数量*/
    private TextView purchaseCountTv;
    /*规格*/
    private TextView purchaseUnitTv;
    /*报价有效期*/
    private TextView expiryDateTv;
    /*交货时间*/
    private TextView deliveryTimeTv;
    /*价格区间*/
    private TextView spriceTv;
    private TextView epriceTv;

    /*制定人*/
    private TextView buyerTv;
    /*计划发起时间*/
    private TextView planCreateTimeTv;
    /*品质*/
    private TextView qualityTv;
    /*品牌*/
    private TextView brandTv;
    //基地图片
    private MyGridView photoGridView;
    private TextView noImageTv;
    private String plan_id;
    private MaterialDialog materialDialog;
    private TextView markerTv;


    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.center_purchase_plan_detail_layout_activity);
        getBundle();
        initToolbar();
        initView();
        initData();
    }


    private void getBundle() {
        type = getIntent().getIntExtra("type", 0);
        status = getIntent().getStringExtra("status");
        if (TextUtils.isEmpty(status)) {
            status = "-1";
        }
        centerPurchasePlanBean = (CenterPurchasePlanBean) getIntent().getSerializableExtra("data");
        plan_id = centerPurchasePlanBean.PLAN_ID;
    }

    private void initView() {
        newBillOfSalesBtn = (Button) findViewById(R.id.newBillOfSalesBtn);
        newBillOfSalesBtn.setOnClickListener(this);

        OriginPlansBtn = (Button) findViewById(R.id.OriginPlansBtn);
        OriginPlansBtn.setOnClickListener(this);

        recallBtn = (Button) findViewById(R.id.recallBtn);
        recallBtn.setOnClickListener(this);
        if (type == 0) {
            if (status.equals("01")) {
                recallBtn.setVisibility(View.VISIBLE);
            }
            if (status.equals("21")) {
                newBillOfSalesBtn.setVisibility(View.VISIBLE);
            }
        } else if (type == 1) {
            newBillOfSalesBtn.setVisibility(View.GONE);
            if (status.equals("11")) {
                recallBtn.setVisibility(View.VISIBLE);
            }
        }
//        else if(type==2){
//            if(status.equals("21")){
//                recallBtn.setVisibility(View.VISIBLE);
//            }
//        }
        producingAreaRbtn = (TextView) findViewById(R.id.producingAreaRbtn);
        homeBaseTv = (TextView) findViewById(R.id.homeBaseTv);
        productNameTv = (TextView) findViewById(R.id.productNameTv);
        purchaseCountTv = (TextView) findViewById(R.id.purchaseCountTv);
        purchaseUnitTv = (TextView) findViewById(R.id.purchaseUnitTv);
        expiryDateTv = (TextView) findViewById(R.id.expiryDateTv);
        deliveryTimeTv = (TextView) findViewById(R.id.deliveryTimeTv);
        spriceTv = (TextView) findViewById(R.id.spriceTv);
        epriceTv = (TextView) findViewById(R.id.epriceTv);
        buyerTv = (TextView) findViewById(R.id.buyerTv);
        markerTv = (TextView) findViewById(R.id.markerTv);
        planCreateTimeTv = (TextView) findViewById(R.id.planCreateTimeTv);
        qualityTv = (TextView) findViewById(R.id.qualityTv);
        brandTv = (TextView) findViewById(R.id.brandTv);
        noImageTv = (TextView) findViewById(R.id.noImageTv);
        photoGridView = (MyGridView) findViewById(R.id.photoGridView);
        initMaterialView();
    }

    private void initMaterialView() {
        materialDialog = new MaterialDialog(context);
        materialDialog.setTitle("撤回");
        materialDialog.content("是否确定撤回");
        materialDialog.btnText("取消", "确定");
        materialDialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                materialDialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                if (type == 0)
                    purchaseManRecall();
                else if (type == 1) {
                    centerManRecall();
                }
            }
        });
    }

    private void initData() {
        if (centerPurchasePlanBean != null) {
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PLACE_NAME))
                producingAreaRbtn.setText(centerPurchasePlanBean.PLACE_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.SITE_NAME))
                homeBaseTv.setText(centerPurchasePlanBean.SITE_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PRODUCTS_NAME))
                productNameTv.setText(centerPurchasePlanBean.PRODUCTS_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PURCHASE_COUNT))
                purchaseCountTv.setText(centerPurchasePlanBean.PURCHASE_COUNT);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.STANDARD))
                purchaseUnitTv.setText(centerPurchasePlanBean.STANDARD);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.EXPIRY_DATE))
                expiryDateTv.setText(centerPurchasePlanBean.EXPIRY_DATE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.DELIVERY_TIME))
                deliveryTimeTv.setText(centerPurchasePlanBean.DELIVERY_TIME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.SPRICE))
                spriceTv.setText(centerPurchasePlanBean.SPRICE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.EPRICE))
                epriceTv.setText(centerPurchasePlanBean.EPRICE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.MAKER))
                markerTv.setText(centerPurchasePlanBean.MAKER);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.BUYER))
                buyerTv.setText(centerPurchasePlanBean.BUYER);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PLAN_CREATE_TIME))
                planCreateTimeTv.setText(centerPurchasePlanBean.PLAN_CREATE_TIME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.QUALITY))
                qualityTv.setText(centerPurchasePlanBean.QUALITY);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.BRAND))
                brandTv.setText(centerPurchasePlanBean.BRAND);
            List<String> images = new ArrayList<>();
            if (centerPurchasePlanBean.PIC != null && centerPurchasePlanBean.PIC.length() > 0) {
                String imageStrs[] = centerPurchasePlanBean.PIC.split(";");
                images = Arrays.asList(imageStrs);
//        photoGridView = (MyGridView) findViewById(R.id.photoGridView);
                GridViewImageNomalAdapter gridViewImageNoAddAdapter = new GridViewImageNomalAdapter(context, images);
                photoGridView.setAdapter(gridViewImageNoAddAdapter);
            } else {
                noImageTv.setVisibility(View.VISIBLE);
            }
        }
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        TextView title = (TextView) findViewById(R.id.titleTv);
        title.setVisibility(View.VISIBLE);
        title.setText("采购计划详情");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.newBillOfSalesBtn:
                Intent intent = new Intent(context, NewBillOfSalesBtnActivity.class);
                intent.putExtra("plan_id", centerPurchasePlanBean.PLAN_ID);
                startActivity(intent);
                break;

            case R.id.OriginPlansBtn:
                getOriginPlans();
                break;
            //撤回
            case R.id.recallBtn:
                materialDialog.show();
                break;
        }
    }

    /**
     * 采购撤回 在中心没有审批之前
     */
    private void purchaseManRecall() {
        HomeDao homeDao = new HomeDao();
        homeDao.updatePlans("02", plan_id, "", new Response.Listener<Success>() {
            @Override
            public void onResponse(Success response) {
                if (response.success) {
                    Toast.makeText(context, "撤回成功！", Toast.LENGTH_LONG).show();
                    materialDialog.dismiss();
                    EventBus.getDefault().post(new PurchaseChangeAddRefreshBean(true));
                    //审批列表刷新
                    popAllActivityExceptOne(MainActivity.class);

                    finish();
                } else {
                    materialDialog.dismiss();
                    Toast.makeText(context, "撤回失败！", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                materialDialog.dismiss();
                Toast.makeText(context, "撤回失败！", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 中心角色撤回操作
     */
    private void centerManRecall() {
        HomeDao homeDao = new HomeDao();
        homeDao.updatePlans("12", plan_id, "", new Response.Listener<Success>() {
            @Override
            public void onResponse(Success response) {
                if (response.success) {
                    Toast.makeText(context, "撤回成功！", Toast.LENGTH_LONG).show();
                    materialDialog.dismiss();
                    EventBus.getDefault().post(new CenterRoleExamineAndApproveListRefreshBean(true));
                    popAllActivityExceptOne(MainActivity.class);

                    finish();
                } else {
                    materialDialog.dismiss();
                    Toast.makeText(context, "撤回失败！", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                materialDialog.dismiss();
                Toast.makeText(context, "撤回失败！", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void getOriginPlans() {
        if (!TextUtils.isEmpty(plan_id)) {
            HomeDao homeDao = new HomeDao();
            homeDao.getOriginPlans(plan_id, new Response.Listener<CenterPurchasePlanListBean>() {
                @Override
                public void onResponse(CenterPurchasePlanListBean response) {
                    if (response.success) {
                        Intent intent1 = new Intent(context, PurchasePriceDetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("data", response.plans.get(0));
                        intent1.putExtras(bundle);
                        startActivity(intent1);
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
        }
    }
}
